﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UserControlFilter.cs" company="None">
//   <Author>Adrian von Allmen, 2013</Author>
// </copyright>
// <summary>
//   Defines the UserControlFilter type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace CrumbleLibrary
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;

    /// <summary>
    /// The user control filter.
    /// </summary>
    public class UserControlFilter : UserControl, IFilterPlugins
    {
        /// <summary>
        /// Collection with dynamically added filter parameter
        /// </summary>
        protected readonly ObservableCollection<FilterItemDynamic> DynamicFilterItemsCollection = new ObservableCollection<FilterItemDynamic>();

        /// <summary>
        /// The filter parameter list.
        /// </summary>
        protected Dictionary<string, object> FilterParameterList = new Dictionary<string, object>();

        /// <summary>
        /// Initializes a new instance of the <see cref="UserControlFilter"/> class.
        /// </summary>
        public UserControlFilter()
        {
        }

        /// <summary>
        /// Get Assembly Build Version.
        /// </summary>
        public virtual string GetVersion
        {
            get
            {
                return this.GetType().Assembly.GetName().Version.ToString();
            }
        }

        /// <summary>
        /// Get the Display Name for Tab or other visual
        /// </summary>
        public string GetDisplayName
        {
            get
            {
                var returnValue = this.GetType().GetFields().FirstOrDefault(item => item.Name == "DisplayName");
                if (returnValue != null && returnValue.GetValue(null) is string)
                {
                    return (string)returnValue.GetValue(null);
                }

                return "No Display Name";
            }
        }

        /// <summary>
        /// Gets a list with all static parameter and their valies
        /// </summary>
        public Dictionary<string, object> StaticParameterList
        {
            get
            {
                return this.FilterParameterList;
            }
        }

        /// <summary>
        /// Gets a list with all dynamically added parameters and their values.
        /// </summary>
        public ObservableCollection<FilterItemDynamic> DynamicParameterList
        {
            get
            {
                return this.DynamicFilterItemsCollection;
            }
        }

        /// <summary>
        /// The set default value.
        /// </summary>
        /// <param name="wpfName"> The ID Name within XAML </param>
        /// <param name="defaultValue"> The default value to attach </param>
        public void SetDefaultValue(string wpfName, object defaultValue)
        {
            var item = this.FindName(wpfName);
            if (item == null)
            {
                return;
            }

            var textBox = item as TextBox;
            if (textBox != null)
            {
                var txt = textBox;
                txt.Text = defaultValue.ToString();
            }

            var picker = item as DatePicker;
            if (picker != null)
            {
                var datePicker = picker;
                datePicker.SelectedDate = DateTime.Parse(defaultValue.ToString());
            }
        }

        /// <summary>
        /// The set filter visibility.
        /// </summary>
        /// <param name="wpfName"> The ID Name within XAML </param>
        /// <param name="isVisible"> Visibility State. </param>
        public void SetFilterVisibility(string wpfName, bool isVisible)
        {
            object myItem = this.FindName(wpfName);
            if (myItem == null)
            {
                return;
            }

            var grid = myItem as Grid;
            if (grid != null)
            {
                grid.Visibility = isVisible ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        /// <summary>
        /// The set filter feature.
        /// </summary>
        /// <param name="wpfName"> The ID Name within XAML </param>
        /// <param name="settingInfo"> The setting info. </param>
        public void SetFilterFeature(string wpfName, string settingInfo)
        {
            if (settingInfo.EndsWith("_SelectionMode"))
            {
                object myItem = this.FindName(wpfName.Replace("_SelectionMode", ""));
                if (myItem == null)
                {
                    return;
                }

                var myListBox = myItem as ListBox;
                if (myListBox != null)
                {
                    myListBox.SelectionMode = (settingInfo == "Single") ? SelectionMode.Single : SelectionMode.Multiple;
                }
            }
        }

        /// <summary>
        /// The attach dynamic filter item.
        /// </summary>
        /// <param name="name"> The name of the dynamic parameter. </param>
        /// <param name="value"> The value. </param>
        public void AttachDynamicFilterItem(string name, FilterItemDynamic value)
        {
            var myItem = this.FindName("DynamicFilterArea");
            if (myItem == null)
            {
                return;
            }

            this.DynamicFilterItemsCollection.Add(value);
        }

        /// <summary>
        /// The change dictionary list.
        /// </summary>
        /// <param name="keyString"> The key string. </param>
        /// <param name="newValue"> The new value. </param>
        protected void ChangeFilterParameter(string keyString, object newValue)
        {
            if (this.FilterParameterList.ContainsKey(keyString))
            {
                this.FilterParameterList.Remove(keyString);
            }

            this.FilterParameterList.Add(keyString, newValue);
        }

        /// <summary>
        /// The save default settings
        /// </summary>
        public virtual void Save()
        {
        }

    }
}
